package com.mini.service;

import com.alibaba.fastjson.JSON;
import com.tencentcloudapi.cdn.v20180606.CdnClient;
import com.tencentcloudapi.cdn.v20180606.models.PurgeUrlsCacheRequest;
import com.tencentcloudapi.cdn.v20180606.models.PurgeUrlsCacheResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * xyf
 */
@Slf4j
@Service
public class TencentCDNService extends BaseService {
    @Value("${tencent.secretId}")
    private String secretId;
    @Value("${tencent.secretKey}")
    private String secretKey;

    /**
     *  刷新
     * @param url
     */
    public void refreshCaches(String url) {

        asyncTaskManager.getExecutor().submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Credential cred = new Credential(secretId, secretKey);

                    HttpProfile httpProfile = new HttpProfile();
                    httpProfile.setEndpoint("cdn.tencentcloudapi.com");

                    ClientProfile clientProfile = new ClientProfile();
                    clientProfile.setHttpProfile(httpProfile);

                    CdnClient client = new CdnClient(cred, "", clientProfile);

                    PurgeUrlsCacheRequest req = new PurgeUrlsCacheRequest();
                    String[] urls1 = {url};

                    req.setUrls(urls1);

                    PurgeUrlsCacheResponse resp = client.PurgeUrlsCache(req);
                    log.info(" tencent cdn refresh response = {} " , JSON.toJSONString(resp));

                } catch (Exception e) {
                    log.error(" tencent cdn refresh cache error ,ex = {} ", e);
                }
            }
        });

    }
//
//    /**
//     *  预热
//     * @param url
//     */
//    public void pushCaches(String url) {
//
//        asyncTaskManager.getExecutor().submit(new Runnable() {
//            @Override
//            public void run() {
//                PushObjectCacheRequest request = new PushObjectCacheRequest();
//                request.setRegionId("cn-hangzhou");
//                request.setObjectPath(url);
//                try {
//                    PushObjectCacheResponse acsResponse = client.getAcsResponse(request);
//                    log.info(" ali cdn push response = {} " , JSON.toJSONString(acsResponse));
//
//                } catch (Exception e) {
//                    log.error(" ali cdn push cache error ,ex = {} ", e);
//                }
//            }
//        });
//
//    }

    public static void main(String[] args) {
        try {
            Credential cred = new Credential("AKID3cx1oLl4LA0av0qOjodnjOwIPFqGj10h", "5c3D9ORDlY7RWVxA7nDNFqDpCJGwOZT2");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("cdn.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            CdnClient client = new CdnClient(cred, "", clientProfile);

            PurgeUrlsCacheRequest req = new PurgeUrlsCacheRequest();
            String[] urls1 = {"https://txtestimg.jiayans.net/testimg.jpg"};

            req.setUrls(urls1);

            PurgeUrlsCacheResponse resp = client.PurgeUrlsCache(req);
            log.info(" tencent cdn refresh response = {} ", JSON.toJSONString(resp));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
